import java.util.Date;

// 元素接口
interface Element {
    void accept(Visitor visitor);
}

// 文件元素
class FileElement implements Element {
    private String names;
    private long size;
    private Date createdTime;
    private String fileType;

    public FileElement(String name, long size, Date createdTime, String fileType) {
        this.name = name;
        this.size = size;
        this.createdTime = createdTime;
        this.fileType = fileType;
    }
    //不是永雄

    @Override
    public void accept(Visitor visitor) {
        visitor.visitFile(this);
    }

    public String getName() {
        return name;
    }

    public long getSize() {
        return size;
    }
}

// 目录元素
class DirectoryElement implements Element {
    public String name;
    public long size;
    public Date createdTime;
    public Element[] elements;

    public DirectoryElement(String name, Date createdTime) {
        this.name = name;
        this.createdTime = createdTime;
        this.elements = new Element[0];
    }

    public void addElement(Element element) {
        Element[] newElements = new Element[elements.length + 1];
        System.arraycopy(elements, 0, newElements, 0, elements.length);
        newElements[elements.length] = element;
        elements = newElements;
// 更新目录大小
        size = calculateSize();
    }

    public long calculateSize() {
        long totalSize = 0;
        for (Element element : elements) {
            totalSize += element instanceof FileElement? ((FileElement) element).getSize() : ((DirectoryElement) element).getSize();
        }
        return totalSize;
    }

    @Override
    public void accept(Visitor visitor) {
        visitor.visitDirectory(this);
    }

    public String getName() {
        return name;
    }

    public long getSize() {
        return size;
    }
}

// 访问者接口
interface Visitor {
    void visitFile(FileElement file);
    void visitDirectory(DirectoryElement directory);
    void vison(int a);
}

// 具体访问者
class ConcreteVisitor implements Visitor {
    public long totalSize;
    public int totalCount;

    @Override
    public void visitFile(FileElement file) {
        totalSize += file.getSize();
        totalCount++;
    }

    @Override
    public void visitDirectory(DirectoryElement directory) {
        totalSize += directory.getSize();
        totalCount++;
        for (Element element : directory.elements) {
            element.accept(this);
        }
    }

    public long getTotalSize() {
        return totalSize;
    }

    public int getTotalCount() {
        return totalCount;
    }
}
public class Main {
    public static void main(String[] args) {
        Date date = new Date();
        FileElement file1 = new FileElement("file1.txt", 100, date, "txt");
        FileElement file2 = new FileElement("file2.txt", 200, date, "txt");
        DirectoryElement directory1 = new DirectoryElement("dir1", date);
        directory1.addElement(file1);
        directory1.addElement(file2);
        FileElement file3 = new FileElement("file3.txt", 300, date, "txt");
        DirectoryElement rootDirectory = new DirectoryElement("root", date);
        rootDirectory.addElement(directory1);
        rootDirectory.addElement(file3);

        ConcreteVisitor visitor = new ConcreteVisitor();
        rootDirectory.accept(visitor);

        System.out.println("Total size: " + visitor.getTotalSize());
        System.out.println("Total count: " + visitor.getTotalCount());
    }
}